enum BTN {
    //% block="开"
    on,
    //% block="关"
    off,
    //% block="清屏"
    clear
}

enum SHOW {
    //% block="亮"
    true,
    //% block="暗"
    false   
}

enum URMUNIT {
    //% block="厘米"
    CM,
    //% block="英寸"
    IN   
}

//% color="#AA278D" iconWidth=50 iconHeight=40
namespace ysgo {
    //% block="init TM1650" blockType="command"
    export function initTM(parameter: any, block: any) {
        Generator.addInclude('tm1650', '#include <TM1650.h>',true);
        Generator.addObject(`mytm`, `TM1650`, `tm1650;`,true);       
        Generator.addSetup(`mytm.init`, `tm1650.init();`,true); 
    }

    //% block="handle TM1650 [BUTTON]" blockType="command"
    //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.on"
    export function handleTM(parameter: any, block: any) {
        let button = parameter.BUTTON.code;
        let code;
        if (button === 'on') {
            code = `tm1650.displayOn();`;
        } else if (button === 'off') {
            code = `tm1650.displayOff();`;
        } else {
            code = `tm1650.clear();`;
        }
        Generator.addInclude('tm1650', '#include <TM1650.h>',true);
        Generator.addObject(`mytm`, `TM1650`, `tm1650;`,true); 
        Generator.addSetup(`mytm.init`, `tm1650.init();`,true); 
        Generator.addCode(code);
    }
    
    //% block="show [STR]" blockType="command"
    //% STR.shadow="string" STR.defl=1234  
    export function printlnTM(parameter: any, block: any) {
        let str = parameter.STR.code;       
        Generator.addInclude('tm1650', '#include <TM1650.h>',true);
        Generator.addObject(`mytm`, `TM1650`, `tm1650;`,true);       
        Generator.addSetup(`mytm.init`, `tm1650.init();`,true);
        Generator.addCode(`tm1650.displayString(${str});`);
    }
    //% block="when press [BUTTON] num [SHOW]" blockType="command"
    //% BUTTON.shadow="dropdown" BUTTON.options="NUMS" BUTTON.defl="NUMS.1"
    //% SHOW.shadow="dropdown" SHOW.options="SHOW" SHOW.defl="SHOW.true"
    export function setDotTM(parameter: any, block: any){
        let num = parameter.BUTTON.code;    
        let show = parameter.SHOW.code;      
        Generator.addInclude('tm1650', '#include <TM1650.h>',true);
        Generator.addObject(`mytm`, `TM1650`, `tm1650;`,true); 
        Generator.addSetup(`mytm.init`, `tm1650.init();`,true); 
        Generator.addCode(`tm1650.setDot(${num}-1, ${show})`);       
    }

    //% block="when press [BUTTON] num  [TRIG]  [ECHO]" blockType="reporter"
    //% BUTTON.shadow="dropdown" BUTTON.options="URMUNIT" BUTTON.defl="URMUNIT.CM"
    //% TRIG.shadow="dropdown" TRIG.options="URMPINX" TRIG.defl="URMPINX.1"
    //% ECHO.shadow="dropdown" ECHO.options="URMPINX" ECHO.defl="URMPINX.2"
    export function getDistance(parameter: any, block: any) {
        let unit = parameter.BUTTON.code;
        let echo = parameter.ECHO.code;
        let trig = parameter.TRIG.code;   
        let code;    
        code =  `urm10.getDistance${unit}(${trig}, ${echo})`;
        Generator.addInclude('urm10', '#include <URM10.h>',true);
        Generator.addObject(`myurm10`, `URM10`, `urm10;`,true);       
        Generator.addCode([code, Generator.ORDER_UNARY_POSTFIX]);
    }
}
